package com.bailan.foreat.listener;

import com.bailan.foreat.entity.Order;
import com.bailan.foreat.mapper.OrderMapper;
import com.bailan.foreat.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Component
public class DelayCancelOrderListener {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderService orderService;

    @RabbitListener(queues = "cancelOrderDeadLetterQueue")
    public void onMsg(Integer orderId, Channel channel, Message message) throws IOException {
        System.out.println("延迟队列的消费者对订单进行处理 orderId: "+orderId);
        // 检查订单状态,在只有在订单还是未支付状态时进行取消
        Order order = orderMapper.selectById(orderId);
        // order状态(0未⽀付,取消(按钮) 1已支付 2已取消(手动,自动)
        if (order.getStatus() == 0){
            orderService.updateOrderStatus(orderId,2);
            System.out.println("订单尚未支付,执行取消操作");

        }else {
            System.out.println("订单已支付,无需操作");
        }
        System.out.println("time2: "+new Date());
        //手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


}
